package bc.swim2.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bc.swim2.ejb.entity.RegisteredUser;
import bc.swim2.ejb.session.FriendshipManagerBeanRemote;
import bc.swim2.ejb.session.UserManagerBeanRemote;

/**
 * Servlet implementation class SearchUsersServlet
 */
public class SearchHelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchHelpServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("methodName").equals("regSearchHelp")) {
			regSearchHelp(request, response);
		} else if (request.getParameter("methodName")
				.equals("notRegSearchHelp")) {
			notRegSearchHelp(request, response);
		}
	}

	private void notRegSearchHelp(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			int abilityId = Integer.parseInt(request
					.getParameter("selectAbility"));

			Object ref2 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref2;

			List<RegisteredUser> users = um
					.getUsersByAbilityIdAllOrFriends(abilityId);

			session.setAttribute("userListTmp2", users);

			response.sendRedirect("MainServlet?to=NotRegSearchHelpResult");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void regSearchHelp(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			int abilityId = Integer.parseInt(request
					.getParameter("selectAbility"));

			String allOrFriends = request.getParameter("allOrFriends");
			List<RegisteredUser> users = null;
			List<RegisteredUser> friends = new ArrayList<RegisteredUser>();

			Object ref2 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref2;

			Object ref3 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref3;

			users = um.getUsersByAbilityIdAllOrFriends(abilityId);

			if (allOrFriends.equals("Friends")) {
				int cuid = ((RegisteredUser) session
						.getAttribute("currentSessionUser")).getId();
				for (RegisteredUser uzer : users) {
					int uid = uzer.getId();
					String fs = fm.getFriendshipState(cuid, uid);
					if (fs.equals("accepted")) {
						friends.add(uzer);
					}
				}
				session.setAttribute("userListTmp1", friends);
			} else {
				session.setAttribute("userListTmp1", users);
			}

			response.sendRedirect("MainServlet?to=SearchHelpAndUsersResult");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
